#!/bin/bash
set -e
name=virtio-net-controller
wd=$(dirname $0)
branch=$(git rev-parse --abbrev-ref HEAD)
sha1=$(git rev-parse HEAD |cut -c -8)
if [ -z "$VER" ] ;then
        VER=$($wd/get_ver.sh)
fi

git archive \
    --format=tar.gz --prefix=$name-$VER/ -o ~/rpmbuild/SOURCES/$name-$VER.tar.gz  HEAD
git submodule init
git submodule update

SPECfile=/dev/shm/$name.spec
[ -e "$SPECfile" ] && rm -f "$SPECfile"
VER=$( $wd/get_ver.sh )
sed -e "s/@PACKAGE_VERSION@/${VER:-2.0.0}/; s/@PACKAGE_REVISION@/${BUILD_NUMBER:-1}/;
        s/@GIT_SHA1@/$sha1/; s/@GIT_BRANCH@/$branch/;" \
     $name.spec.in > $SPECfile

# BUILD_NUMBER is an env var passed by Jenkins
# https://stackoverflow.com/questions/16155792/using-jenkins-build-number-in-rpm-spec-file
rpmbuild -bs $SPECfile
